package cn.edu.ujn.bmis.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.ujn.bmis.model.Inform;
import cn.edu.ujn.bmis.model.UserInfo;
import cn.edu.ujn.bmis.service.InformManager;
import cn.edu.ujn.bmis.service.UserInfoManager;
import cn.edu.ujn.bmis.tools.Helper;

import com.opensymphony.xwork2.ActionSupport;

@Component("Inform")
@Scope("prototype")
public class InformAction extends ActionSupport {

	private static final long serialVersionUID = 723492243696848249L;
	private InformManager informManager;
	private Inform inform;
	private List<Inform> informList = new ArrayList<Inform>();
	private Helper helper;
	private String userId;
	private String name;
	private int pageNum = 1;
	private int currentPage = 1; // 当前是第几页
	private int totalCount = 0; // 总条数
	private int numPerPage = 5; // 每页显示多少条
	private int pageNumShown = 5; // 页标数字多少个
	private int pageCount = 0; // 总页数
	private String orderField;
	private String orderDirection;
	private String targetType = "navTab"; // navTab或dialog，用来标记是navTab上的分页还是dialog上的分页
	private long id;
    private String ids;
    private String content;
    private String functionNum;
    private UserInfoManager userInfoManager;
    
	public String beforeAddInform(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		return SUCCESS;
	}

	public void addInform() {
		//检查输入的读者编号是否存在
		String hql_check="";
		List<UserInfo> userInfoList=new ArrayList<UserInfo>();
		if(inform.getUserInfo().getUserId()!=null&&!"".equals(inform.getUserInfo().getUserId())){
			hql_check="from UserInfo u where u.userId='"+inform.getUserInfo().getUserId()+"'";
			userInfoList=userInfoManager.findUserInfo(hql_check);
			if(userInfoList.size()<=0){
				Helper.out("300", "输入的用户编号不存在请检查！", "", "", "", "");
				return;
			}
		}
		if (informManager.saveInform(inform)) {
			Helper.out("200", "通告添加成功", "closeCurrent", "", "", "findInform");
		} else {
			Helper.out("300", "通告添加失败", "", "", "", "");
		}
	}
	
	public String findReaderInform(){
		String hql="";
		hql=" from Inform i where i.id="+id+"";
		informList=informManager.findInform(hql);
		if(informList.size()>0){
			inform=informList.get(0);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除通告
	 */
	public void deleteInform() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
			return;
		}
		String hql = "";
		hql = "delete from Inform b where id in (" + ids + ")";
		if (informManager.update(hql)) {
			Helper.out("200", "通告删除成功", "", "", "", "findInform");
		} else {
			Helper.out("300", "通告删除失败", "", "", "", "");
		}
	}
	
	public String findDetailInform(){
		String hql = "from Inform i where i.id=" + id + "";
		informList = informManager.findInform(hql);
		if (informList.size() > 0) {
			inform = informList.get(0);
		}
		return SUCCESS;
	}
	
	public String beforeUpdateInform(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql = "from Inform i where i.id=" + id + "";
		informList = informManager.findInform(hql);
		if (informList.size() > 0) {
			inform = informList.get(0);
		}
		return SUCCESS;
	}
	
	public void updateInform(){
		String hql="";
		//检查输入的读者编号是否存在
		String hql_check="";
		List<UserInfo> userInfoList=new ArrayList<UserInfo>();
		if(userId!=null&&!"".equals(userId)){
			hql_check="from UserInfo u where u.userId='"+userId.trim()+"'";
			userInfoList=userInfoManager.findUserInfo(hql_check);
			if(userInfoList.size()<=0){
				Helper.out("300", "输入的用户编号不存在请检查！", "", "", "", "");
				return;
			}
		}
		if(userId!=null&&!"".equals(userId)){
			hql="update Inform i set i.userInfo.userId='"+userId.trim()+"',i.name='"+name.trim()+"',i.content='"+content.trim()+"' where i.id="+id+"";
		}else{
			hql="update Inform i set i.userInfo.userId=null,i.name='"+name.trim()+"',i.content='"+content.trim()+"' where i.id="+id+"";
		}
		if (informManager.update(hql)) {
			Helper.out("200", "通告修改成功", "closeCurrent", "", "", "findInform");
		} else {
			Helper.out("300", "通告修改失败", "", "", "", "");
		}
	}

	@SuppressWarnings("unchecked")
	public String findInform() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql = "";
		String hql_size = "";
		this.currentPage = this.pageNum;
		if(userId != null && !"".equals(userId)){
			hql = "select DISTINCT i from Inform i ,UserInfo u where ((i.userInfo.userId=u.userId and u.userId='"+userId.trim()+"') or (i.userInfo.userId=null)) ";
			hql_size = "select count(DISTINCT i)  from Inform i ,UserInfo u where ((i.userInfo.userId=u.userId and u.userId='"+userId.trim()+"') or (i.userInfo.userId=null)) ";
		}else{
			hql = "select  i from Inform i where i.id=i.id";
			hql_size = "select count(*)  from Inform i where i.id=i.id";
		}
		if (name != null && !"".equals(name)) {
			hql += " and i.name='" + name.trim() + "'";
			hql_size += " and i.name='" + name.trim() + "'";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		informList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}

	public InformManager getInformManager() {
		return informManager;
	}

	@Resource(name = "informManager")
	public void setInformManager(InformManager informManager) {
		this.informManager = informManager;
	}

	public Inform getInform() {
		return inform;
	}

	public void setInform(Inform inform) {
		this.inform = inform;
	}

	public List<Inform> getInformList() {
		return informList;
	}

	public Helper getHelper() {
		return helper;
	}

	@Resource(name = "helper")
	public void setHelper(Helper helper) {
		this.helper = helper;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public String getTargetType() {
		return targetType;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}

	public String getIds() {
		return ids;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFunctionNum() {
		return functionNum;
	}

	public void setFunctionNum(String functionNum) {
		this.functionNum = functionNum;
	}

	public UserInfoManager getUserInfoManager() {
		return userInfoManager;
	}

	@Resource(name="userInfoManager")
	public void setUserInfoManager(UserInfoManager userInfoManager) {
		this.userInfoManager = userInfoManager;
	}
	
}
